package com.caochenlei.graph;

import java.util.Stack;

public class DijkstraSPTest {
    public static void main(String[] args) {
        int s = 0;
        int n = 8;
        EdgeWeightedDigraph EWD = new EdgeWeightedDigraph(n);
        EWD.addEdge(new DirectedEdge(4, 5, 0.35));
        EWD.addEdge(new DirectedEdge(5, 4, 0.35));
        EWD.addEdge(new DirectedEdge(4, 7, 0.37));
        EWD.addEdge(new DirectedEdge(5, 7, 0.28));
        EWD.addEdge(new DirectedEdge(7, 5, 0.28));
        EWD.addEdge(new DirectedEdge(5, 1, 0.32));
        EWD.addEdge(new DirectedEdge(0, 4, 0.38));
        EWD.addEdge(new DirectedEdge(0, 2, 0.26));
        EWD.addEdge(new DirectedEdge(7, 3, 0.39));
        EWD.addEdge(new DirectedEdge(1, 3, 0.29));
        EWD.addEdge(new DirectedEdge(2, 7, 0.34));
        EWD.addEdge(new DirectedEdge(6, 2, 0.40));
        EWD.addEdge(new DirectedEdge(3, 6, 0.52));
        EWD.addEdge(new DirectedEdge(6, 0, 0.58));
        EWD.addEdge(new DirectedEdge(6, 4, 0.93));

        DijkstraSP sp = new DijkstraSP(EWD, s);
        for (int v = 0; v < n; v++) {
            if (sp.hasPathTo(v)) {
                String distTo = String.format("%.2f", sp.distTo(v));
                System.out.println("顶点" + s + "到顶点" + v + "的最短路径树如下，最短路径的总权重为：" + distTo);
                Stack<DirectedEdge> stack = sp.pathTo(v);
                while (!stack.isEmpty()) {
                    System.out.println(stack.pop());
                }
                System.out.println();
            }
        }
    }
}
